package userhandler

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/dao/memberdao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/usersteward"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// HandleGetMembersList 获取会员列表
func HandleGetMembersList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetMembersListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := memberdao.NewMemberDao().GetAdminMemberList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

func SetStewardWorkforceStaff(ctx *gin.Context) {
	loginUserId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.SetStewardWorkforceStaffReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = loginUserId
	param.Ip = ctx.ClientIP()

	if data := dao.NewUserDao().SetStewardWorkforceStaff(param); data.Err != nil {
		Response(ctx)("", CodeInternalServerError, false)(OK)
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// SetStewardPlaceStaff 设置网点员工
func SetStewardPlaceStaff(ctx *gin.Context) {
	loginUserId, err := helper.GetUserIdFromCtx(ctx) // 获取当前登录用户ID
	dingo.ErrorIf(err).Unwrap()

	param := &request.SetStewardPlaceStaffReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = loginUserId
	param.Ip = ctx.ClientIP()

	if data := dao.NewUserDao().SetStewardPlaceStaff(param); data.Err != nil {
		Response(ctx)("", CodeInternalServerError, false)(OK)
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleGetStewardPlaceUserList 获取员工列表
func HandleGetStewardPlaceUserList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StStaffListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	//if param.IsAdmin == helper.TypeNumIsAdminOperator {
	//param.LoginUserId = userId
	//}

	data := dao.NewUserDao().GetStewardPlaceStaffUserList(param)

	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"list": data.Unwrap(),
		"ids":  data.Val(),
	})(OK)
}

// HandleGetStewardUserList 获取员工列表
func HandleGetStewardUserList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StStaffListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewUserDao().GetStewardStaffUserList(param)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// HandleInsertStewardUser 新建员工帐号
func HandleInsertStewardUser(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &usersteward.StUserStaff{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	if param.ParentId == 0 {
		param.ParentId = userId
	}

	if data := dao.NewUserDao().InsertStewardStaffUser(param); data.Err != nil {
		Response(ctx)(data.Err.Error(), CodeInternalServerError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleUpdateStewardUser 修改员工帐号
func HandleUpdateStewardUser(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &usersteward.StUserStaff{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	if param.ParentId == 0 {
		param.ParentId = userId
	}

	if data := dao.NewUserDao().UpdateStewardStaffUser(param, 0); data.Err != nil {
		Response(ctx)(data.Err.Error(), CodeInternalServerError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleDeleteStewardUser 删除员工帐号
func HandleDeleteStewardUser(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()
	IsAdmin, _ := helper.GetIsAdminFromCtx(ctx)

	param := &usersteward.StUserStaff{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.ParentId = userId

	if data := dao.NewUserDao().DeleteStewardStaffUser(param, IsAdmin); data.Err != nil {
		Response(ctx)(data.Err.Error(), CodeInternalServerError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}
